package com.axxc.jpa.annotation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class EmailValidator implements ConstraintValidator<EmailValid, Object> {
	private static final Logger logger = LoggerFactory.getLogger(EmailValid.class);
	private String email;
	private boolean allowNull;

	public void initialize(EmailValid constraintAnnotation) {
		this.email = constraintAnnotation.email();
		this.allowNull = constraintAnnotation.allowNull();
	}

	public boolean isValid(Object value, ConstraintValidatorContext context) {
		try {
			if (this.allowNull || StringUtils.isBlank(String.valueOf(value))) {
				return true;
			}
			return RegularExpressionUtil.matchEmail(value.toString());
		} catch (Exception var4) {
			logger.error("EmailValidator isValid error codelist =={},value==>{},error:{}", this.email, value, var4);
			return false;
		}
	}
}